package com.bdit.ydcb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bdit.ydcb.R;
import com.bdit.ydcb.model.WaterUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shengpj on 2017/7/5.
 */

public class WaterUserListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<WaterUser> dataList = new ArrayList<>();
    private ItemClickListener itemClickListener;

    public void addAllData(List<WaterUser> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.dataList.clear();
    }

    public WaterUserListAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView username;
        public TextView userno;
        public TextView address;
        public ImageView state;
        public ViewHolder(View itemView) {
            super(itemView);
            username = (TextView) itemView.findViewById(R.id.user_name);
            userno = (TextView) itemView.findViewById(R.id.user_no);
            address = (TextView) itemView.findViewById(R.id.user_address);
            state = (ImageView) itemView.findViewById(R.id.state);
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    itemClickListener.onItemClick(v,getPosition());
                }
            });
        }
    }


    public class  EmptyHolder extends RecyclerView.ViewHolder{
        public EmptyHolder (View itemView){
            super(itemView);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType == -1) {
            v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_data, parent, false);
            return new EmptyHolder(v);
        }else{
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_water_user2, parent, false);
            return new ViewHolder(v);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (dataList.size() < 1) {
            return -1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof ViewHolder){
            ViewHolder  vh= ((ViewHolder)holder);
            vh .username.setText(dataList.get(position).getUserName());
            vh.userno.setText(dataList.get(position).getUserNo());
            vh.address.setText(dataList.get(position).getAddress());
            vh.state.setImageResource(dataList.get(position).getState().equals("0")?R.drawable.cb_state_wc:R.drawable.cb_state_yc);
        }

    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return dataList.size() > 0 ? dataList.size() : 1;
    }

    public List<WaterUser> getDataList() {
        return dataList;
    }

    public interface ItemClickListener{
        public void onItemClick(View view,int position);
    }
}
